package com.wangjiyuan.giftshuo.ui.fragment.listtop.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.bean.listtopbean.everydaybean.EveryDayBean;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.helper.RecyclerViewAdapterHelper;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.interfaces.MyItemListener;

import java.util.List;

/**
 * Created by ZangSyeriong on 2016/11/15.
 *
 * RecyclerView适配器
 */

public class RecyclerViewAdapter extends RecyclerViewAdapterHelper<Object>{

    public static final int HEADER = 0, BODY = 1;
    private MyItemListener myItemListener;

    public RecyclerViewAdapter(Context context, List<Object> list, MyItemListener myItemListener) {
        super(context, list);
        this.myItemListener = myItemListener;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    //获取子布局的对应类型
    @Override
    public int getItemViewType(int position) {
        Object object = mList.get(position);
        if (object instanceof EveryDayBean.DataBean){
            return HEADER;
        }
        return BODY;
    }

    //根据类型添加子布局
    @Override
    public RecyclerView.ViewHolder onCreateMyViewHolder(ViewGroup parent, int viewType) {
            View view = null;
            switch (viewType){
                case HEADER:
                    view = mInflater.inflate(R.layout.item_listtop_recyclerview_header, parent, false);
                    return new MyHeaderViewHolder(view);
                case BODY:
                    view = mInflater.inflate(R.layout.item_listtop_recyclerview, parent, false);
                    return new MyViewHolder(view);
            }
        return null;
    }


    //根据对应的子布局添加数据
    @Override
    public void onBindMyViewHolder(RecyclerView.ViewHolder holder, final int position) {
        int widthPixels = mContext.getResources().getDisplayMetrics().widthPixels;

        if (holder instanceof MyHeaderViewHolder){
            EveryDayBean.DataBean dataBean = (EveryDayBean.DataBean) mList.get(position);
            Glide.with(mContext)
                    .load(dataBean.getCoverWebp())
                    .placeholder(R.mipmap.post_place_holder_image)
                    .centerCrop()
                    .into(((MyHeaderViewHolder) holder).img_item_lt_header);
        }else if (holder instanceof MyViewHolder){
            ((MyViewHolder)holder).item_rv_ll.setLayoutParams(new LinearLayout.LayoutParams(
                    widthPixels/2, ViewGroup.LayoutParams.WRAP_CONTENT));
            EveryDayBean.DataBean.ItemsBean itemsBean = (EveryDayBean.DataBean.ItemsBean) mList.get(position);
                Glide.with(mContext)
                        .load(itemsBean.getCoverWebpUrl())
                        .placeholder(R.mipmap.post_place_holder_image)
                        .into(((MyViewHolder) holder).img_rv_item);

                ((MyViewHolder) holder).tv_rv_item_title.setText(
                        itemsBean.getShortDescription());

                ((MyViewHolder) holder).tv_rv_item_context.setText(
                        itemsBean.getName());

                ((MyViewHolder) holder).tv_rv_item_price.setText(
                        "¥"+itemsBean.getPrice());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    myItemListener.getPosition(position);
                }
            });
        }
    }

    //RecyclerView中GrideLayout的布局
    static class MyViewHolder extends RecyclerView.ViewHolder {

        private LinearLayout item_rv_ll;
        private ImageView img_rv_item;
        private TextView tv_rv_item_title, tv_rv_item_context, tv_rv_item_price;

        public MyViewHolder(View itemView) {
            super(itemView);

            item_rv_ll = (LinearLayout) itemView.findViewById(R.id.item_rv_ll);
            img_rv_item = (ImageView) itemView.findViewById(R.id.img_rv_item);
            tv_rv_item_title = (TextView) itemView.findViewById(R.id.tv_rv_item_title);
            tv_rv_item_context = (TextView) itemView.findViewById(R.id.tv_rv_item_context);
            tv_rv_item_price = (TextView) itemView.findViewById(R.id.tv_rv_item_price);

        }
    }

    //RecyclerView中最顶部的图片布局
    static class MyHeaderViewHolder extends RecyclerView.ViewHolder{

        private ImageView img_item_lt_header;

        public MyHeaderViewHolder(View itemView) {
            super(itemView);

            img_item_lt_header = (ImageView) itemView.findViewById(R.id.img_item_lt_header);
        }
    }
}
